/**
 * 
 */
package com.google.code.cisx.store;

import javax.persistence.EntityManager;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.google.code.cisx.model.project.Project;
import com.google.code.cisx.model.project.ProjectGroup;
import com.google.code.cisx.store.api.Query;
import com.google.code.cisx.store.api.Store;
import com.google.code.cisx.store.jpa.JpaProvider;
import com.google.code.cisx.store.jpa.JpaStore;
import com.google.code.cisx.store.tx.TransactionInterceptor;
import com.google.code.cisx.store.tx.annotation.Transactional;
import com.google.inject.AbstractModule;
import com.google.inject.TypeLiteral;
import com.google.inject.matcher.Matchers;

/**
 * @author <a href='mailto:rahul.thakur.xdev@gmail.com'>Rahul Thakur</a>
 * @version $Id: StoreModule.java 258 2008-11-29 06:23:51Z rahul.thakur.xdev $
 * @since 1.0
 */
public class StoreModule extends AbstractModule {

  /**
   * Commons Logging.
   */
  private static final Log log = LogFactory.getLog(StoreModule.class);

  private TransactionInterceptor txnInterceptor = new TransactionInterceptor();

  @Override
  protected void configure() {
    log.debug("Configuring module: " + StoreModule.class.getName());

    bind(EntityManager.class).toProvider(JpaProvider.class).asEagerSingleton();

    bindInterceptor(Matchers.any(), Matchers.annotatedWith(Transactional.class), txnInterceptor);

    bind(new TypeLiteral<Store<Project, Query<Project>>>() {
    }).to(new TypeLiteral<JpaStore<Project, Query<Project>>>() {
    });

    bind(new TypeLiteral<Store<ProjectGroup, Query<ProjectGroup>>>() {
    }).to(new TypeLiteral<JpaStore<ProjectGroup, Query<ProjectGroup>>>() {
    });

    /*
    bind(new TypeLiteral<Store<ProjectNotifier, Query<ProjectNotifier>>>() {
    }).to(new TypeLiteral<JpaStore<ProjectNotifier, Query<ProjectNotifier>>>() {
    });

    bind(new TypeLiteral<Store<BuildDefinition, Query<BuildDefinition>>>() {
    }).to(new TypeLiteral<JpaStore<BuildDefinition, Query<BuildDefinition>>>() {
    });
*/
    bind(TransactionInterceptor.class).toInstance(txnInterceptor);

    requestStaticInjection(StoreService.class);
  }
}
